<template>
  <div>
    <jy-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="父id" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入父id" />
        </el-form-item> -->
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input type="number" v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </jy-dialog>
  </div>
</template>
<script>
import { listBsFbfx, getBsFbfx, delBsFbfx, addBsFbfx, updateBsFbfx } from "@/api/pl/bsFbfx";

export default {
  name: 'fbfx-add-or-update',
  props: {
    zdId: {
      type: [String, Number],
      default: undefined
    }
  },
  data () {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        zdId: undefined,
        parentId: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        zdId: [
          { required: true, message: "所属站点id不能为空", trigger: "blur" }
        ],
        parentId: [
          { required: true, message: "父id不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型(1:分部；2:分项)不能为空", trigger: "change" }
        ],
      }
    };
  },
  created () {
  },
  mounted () {
  },
  methods: {
    init (info) {
      const { parentId, id } = info;
      this.reset();
      this.form.zdId = this.zdId;
      if (id) {
        this.form.id = id;
        this.getInfo();
        this.title = "修改分部分项";
      } else {
        this.form.parentId = parentId;
        this.title = "新增分部分项";
      }
      this.open = true;
    },
    getInfo () {
      this.loading = true;
      getBsFbfx(this.form.id).then(response => {
        this.loading = false;
        this.form = response.data;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: undefined,
        zdId: this.zdId,
        parentId: undefined,
        name: undefined,
        type: undefined,
        remark: undefined,
        sort: undefined,
      };
      this.resetForm("form");
    },

    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateBsFbfx(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.cancel();
              this.$emit('callBack');
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addBsFbfx(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.cancel();
              this.$emit('callBack');
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped></style>
